<!--  -->
<template>
  <div class="">
    <h2>机构管理</h2>
    <el-button type="primary" style="margin:20px" @click="handleOpenDialog('添加机构',1,0)">添加机构</el-button>
    <div id="search-card">
      <div id="search-content">
        <el-form v-model="search" :inline="true">

          <el-form-item label="机构名称">
            <el-input v-model="search.orgName" />
          </el-form-item>

          <el-form-item label="机构分类">
            <!-- <el-input v-model="search.orgType" /> -->
            <el-select v-model="search.orgType">
              <el-option
                v-for="otype in orgT"
                :key="otype.id"
                :value="otype.id"
                :label="otype.typeName"
              />
            </el-select>
          </el-form-item>

          <el-form-item>
            <el-button type="primary" style="margin-left:10px" @click="conditionQuery">查找</el-button>
          </el-form-item>

          <el-form-item label="模糊查询">
            <el-input v-model="mohu" />
          </el-form-item>

          <el-form-item>
            <el-button type="primary" style="margin-left:10px" @click="mohuQuery">查找</el-button>
          </el-form-item>
        </el-form>
      </div>

    </div>

    <div id="table-area">
      <el-table
        v-loading="listLoading"
        :data="list.slice((currentPage-1)*pagesize,currentPage*pagesize)"
        element-loading-text="Loading"
        border
        fit
        highlight-current-row
      >
        <!-- <el-table-column
          align="center"
          prop="orgId"
          label="机构编号"
          min-width="10%"
        /> -->

        <el-table-column
          label="序号"
          type="index"
          min-width="8%"
          align="center"
        >
          <template scope="scope">
            <span>{{ (currentPage - 1) * pagesize + scope.$index + 1 }}</span>
          </template>
        </el-table-column>

        <el-table-column
          align="center"
          prop="orgName"
          label="机构名称"
          min-width="20%"
        />

        <el-table-column
          align="center"
          prop="typeName"
          label="机构分类"
          min-width="10%"
        />

        <!-- <el-table-column
          align="center"
          prop="orgImg"
          label="机构Logo"
          min-width="15%"
        >
          <template slot-scope="Scope">
            <img :src="Scope.row.orgImg" width="160px" height="40px">
          </template>
        </el-table-column> -->

        <el-table-column
          align="center"
          prop="consoleUser.username"
          label="负责人"
          min-width="6%"
        />

        <el-table-column
          align="center"
          prop="contactTel"
          label="联系电话"
          min-width="10%"
        />

        <el-table-column
          align="center"
          prop="contacts"
          label="联系人"
          min-width="6%"
        />

        <el-table-column
          align="center"
          prop="contactNumber"
          label="联系人电话"
          min-width="10%"
        />

        <el-table-column
          align="center"
          label="二维码"
          min-width="10%"
        >
          <template slot-scope="Scope">
            <vue-qr :text="String(Scope.row.orgId)" :size="60" :margin="0" />
          </template>
        </el-table-column>
        <!-- <el-table-column
          align="center"
          prop="shareRatio"
          label="分账比例"
          min-width="8%"
        /> -->

        <el-table-column align="center" label="操作" min-width="20%">
          <template slot-scope="Scope">
            <el-button type="primary" icon="el-icon-edit" @click="handleOpenDialog('编辑机构',2,Scope.row.orgId)">编辑</el-button>

            <el-button type="danger" icon="el-icon-delete" @click="handleDelete(Scope.row.orgId)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>

      <div id="pages">
        <el-pagination
          :current-page="currentPage"
          :page-sizes="[5,10]"
          :page-size="pagesize"
          layout="total, sizes,prev, pager, next"
          :total="list.length"
          prev-text="上一页"
          next-text="下一页"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
        />
      </div>
      <orgDetail :id="id" :title="title" :type="type" :visible="dialogFormVisible" @close="handleClose" />
    </div>

  </div>

</template>

<script>
// 这里可以导入其他文件（比如：组件，工具js，第三方插件js，json文件，图片文件等等）
// 例如：import 《组件名称》 from '《组件路径》';
import { getAllOrg, deleteById, conditionSearch, mohuSearch } from '@/api/org'
import orgDetail from '@/views/org/orgDetail'
import { getAll } from '@/api/classType'
import VueQr from 'vue-qr'
export default {
  name: 'Org',
  // import引入的组件需要注入到对象中才能使用
  components: {
    'orgDetail': orgDetail,
    VueQr
  },
  data() {
    // 这里存放数据
    return {
      currentPage: 1, // 默认显示页面为1
      pagesize: 5, //    每页的数据条数
      list: [],
      listLoading: false,
      dialogFormVisible: false,
      org: {},
      title: '添加校区',
      type: 1, // 1 添加 2 修改
      id: 1,
      search: {},
      mohu: '',
      orgT: {}
    }
  },
  // 监听属性 类似于data概念
  computed: {},
  // 监控data中的数据变化
  watch: {},
  // 生命周期 - 创建完成（可以访问当前this实例）
  created() {
    this.fetchData()
    this.getOrgType()
  },
  // 生命周期 - 挂载完成（可以访问DOM元素）
  mounted() {},
  beforeCreate() {}, // 生命周期 - 创建之前
  beforeMount() {}, // 生命周期 - 挂载之前
  beforeUpdate() {}, // 生命周期 - 更新之前
  updated() {}, // 生命周期 - 更新之后
  beforeDestroy() {}, // 生命周期 - 销毁之前
  destroyed() {}, // 生命周期 - 销毁完成
  activated() {},
  // 方法集合
  methods: {
    handleSizeChange: function(size) {
      this.pagesize = size
    },
    // 点击第几页
    handleCurrentChange: function(currentPage) {
      this.currentPage = currentPage
    },
    handleClick(row) {
      console.log(row)
    },
    fetchData() {
      getAllOrg().then((resp) => {
        this.list = resp.obj
        console.log(this.list)
      })
      this.listLoading = false
    },
    getOrgType() {
      getAll().then((resp) => {
        this.orgT = resp.obj
      })
    },
    handleOpenDialog(title, type, id) {
      this.title = title
      this.type = type
      if (type === 2) { this.id = id }
      this.dialogFormVisible = true
    },
    handleClose() {
      this.dialogFormVisible = false
    },
    handleDelete(id) {
      this.$confirm('确认删除?').then(() => {
        deleteById(id).then(() => {
          this.$notify({
            title: '删除成功',
            type: 'success'
          })
          const totalPage = Math.ceil((this.list.length - 1) / this.pagesize)
          const page = this.currentPage > totalPage ? totalPage : this.currentPage
          this.currentPage = this.currentPage < 1 ? 1 : page
          this.fetchData()
        })
      })
    },
    conditionQuery() {
      console.log(this.search)
      conditionSearch(this.search).then((data) => {
        this.list = data.obj
        this.search = {}
        this.currentPage = 1
      })
    },
    mohuQuery() {
      console.log(this.mohu)
      mohuSearch(this.mohu).then((data) => {
        this.list = data.obj
        this.currentPage = 1
      })
    }

  } // 如果页面有keep-alive缓存功能，这个函数会触发
}
</script>
<style  scoped>
#main{
  margin-left: 5px;
  margin-right: 5px;
}
#table-area{
  margin-left: 10px;
  margin-right: 10px;
}
#search-card {
  margin-left: 50px;
  width: 100%;
  height: 80px;
  overflow-x: hidden;
}
</style>
